using System;
using System.Threading.Tasks;

namespace AsyncPowerCalculation
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("异步计算表达式: a^58 + b^50");
            Console.WriteLine("其中a和b都是大于1小于10的实数");
            Console.WriteLine("==================================\n");

            try
            {
                // 获取用户输入的a和b值
                double a = GetInputValue("a");
                double b = GetInputValue("b");

                Console.WriteLine($"\n开始计算: {a}^58 + {b}^50");
                Console.WriteLine("正在异步计算中...");

                // 记录开始时间
                DateTime startTime = DateTime.Now;

                // 异步计算a^58和b^50
                Task<double> taskA = CalculatePowerAsync(a, 58);
                Task<double> taskB = CalculatePowerAsync(b, 50);

                // 等待两个计算都完成并获取结果
                double resultA = await taskA;
                double resultB = await taskB;

                // 计算总和
                double sum = resultA + resultB;

                // 记录结束时间
                DateTime endTime = DateTime.Now;
                TimeSpan duration = endTime - startTime;

                // 显示结果
                Console.WriteLine($"\n计算结果:");
                Console.WriteLine($"{a}^58 = {resultA}");
                Console.WriteLine($"{b}^50 = {resultB}");
                Console.WriteLine($"{a}^58 + {b}^50 = {sum}");
                Console.WriteLine($"\n计算耗时: {duration.TotalMilliseconds} 毫秒");

                // 演示使用Task.WhenAll同时等待多个任务
                Console.WriteLine("\n使用Task.WhenAll重新计算...");
                startTime = DateTime.Now;
                
                Task<double>[] tasks = { CalculatePowerAsync(a, 58), CalculatePowerAsync(b, 50) };
                double[] results = await Task.WhenAll(tasks);
                sum = results[0] + results[1];
                
                endTime = DateTime.Now;
                duration = endTime - startTime;
                
                Console.WriteLine($"使用Task.WhenAll计算结果: {sum}");
                Console.WriteLine($"计算耗时: {duration.TotalMilliseconds} 毫秒");

            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }

            Console.WriteLine("\n按任意键退出...");
            Console.ReadKey();
        }

        /// <summary>
        /// 异步计算幂运算
        /// </summary>
        /// <param name="baseValue">底数</param>
        /// <param name="exponent">指数</param>
        /// <returns>计算结果</returns>
        private static async Task<double> CalculatePowerAsync(double baseValue, int exponent)
        {
            // 使用Task.Run在后台线程中执行计算
            return await Task.Run(() => 
            {
                // 计算幂运算
                return Math.Pow(baseValue, exponent);
            });
        }

        /// <summary>
        /// 获取用户输入的数值，并验证是否在1到10之间
        /// </summary>
        /// <param name="variableName">变量名</param>
        /// <returns>用户输入的有效数值</returns>
        private static double GetInputValue(string variableName)
        {            
            while (true)
            {                
                Console.Write($"请输入{variableName}的值 (1 < {variableName} < 10): ");
                string input = Console.ReadLine();
                
                if (double.TryParse(input, out double value))
                {                    
                    if (value > 1 && value < 10)
                    {                        
                        return value;
                    }                    
                    else                    
                    {                        
                        Console.WriteLine($"错误：{variableName}的值必须大于1且小于10，请重新输入。");
                    }                
                }
                else                
                {                    
                    Console.WriteLine("错误：请输入有效的数字。");                
                }            
        }
    }
}